package com.jinlejia.erp.adapter;

import android.content.Context;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemAdjustmentRecordLayoutBinding;
import com.jinlejia.erp.utils.DateUtil;
import com.jinlejia.erp.utils.StringUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Map;


/**
 * 调价记录Adapter
 */
public class AdjustmentRecordAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;
    private String type;

    public AdjustmentRecordAdapter(ArrayList<Map<String, Object>> data, String type) {
        super(data);
        this.type = type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adjustment_record_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {

        ItemAdjustmentRecordLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);

        binding.time.setText(DateUtil.getTime(StringUtil.valueOf(mData.get(i).get("createTime"),"0"), "yyyy-MM-dd HH:mm"));
        String str = "";
        double p = Double.parseDouble(StringUtil.valueOf(mData.get(i).get("priceEnd"))) - Double.parseDouble(StringUtil.valueOf(mData.get(i).get("priceStart")));
        java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.00");
        if (type.equals("shs")) {
            //出售
            if (p > 0) {
                //涨价
                str = str.concat(myformat.format(Double.valueOf(StringUtil.valueOf(mData.get(i).get("priceEnd"))))).concat("万").concat("(<font color=\"#FF0000\">+").concat(myformat.format(p)).concat("</font>万)");
            } else {
                str = str.concat(myformat.format(Double.valueOf(StringUtil.valueOf(mData.get(i).get("priceEnd"))))).concat("万").concat("(<font color=\"#0B9800\">").concat(myformat.format(p)).concat("</font>万)");
            }
        } else {
            if (p > 0) {
                //涨价
                str = str.concat(myformat.format(Double.valueOf(StringUtil.valueOf(mData.get(i).get("priceEnd"))))).concat("元").concat("(<font color=\"#FF0000\">+").concat(myformat.format(p)).concat("</font>元)");
            } else {
                str = str.concat(myformat.format(Double.valueOf(StringUtil.valueOf(mData.get(i).get("priceEnd"))))).concat("元").concat("(<font color=\"#0B9800\">").concat(myformat.format(p)).concat("</font>元)");
            }
        }
        binding.change.setText(Html.fromHtml(str));
        binding.store.setText(StringUtil.valueOf(mData.get(i).get("updateUserName")) + "  " + StringUtil.valueOf(mData.get(i).get("updateDept")));
        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }


}
